#include "stdafx.h"
#include "ui_button.h"
#include "ui_label.h"

_SSL_BEGIN
_SSUIL_BEGIN

UIButton::UIButton()
{

}

UIButton::~UIButton()
{

}

bool UIButton::ApplyAttributes(IUIBuilderInterface *ui_builder, 
	IUIXMLDocumentInterface *xml_document, UIXmlNode node)
{
	if(!__super::ApplyAttributes(ui_builder, xml_document, node))
		return false;

	UIXmlAttribute attribute = NULL;

	//
	attribute = xml_document->FirstAttribute(node, "text");
	if(attribute != NULL)
	{
		SetText(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
	}

	return true;
}

void UIButton::AfterBuild(IUIBuilderInterface *ui_builder,
	IUIXMLDocumentInterface *xml_document, UIXmlNode node)
{
	__super::AfterBuild(ui_builder, xml_document, node);

	UIXmlAttribute attribute = NULL;

	//
	attribute = xml_document->FirstAttribute(node, "label");
	if(attribute != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> control = FindControl(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
		if(control != NULL)
		{
			UILabel *lbl = dynamic_cast<UILabel*>(control.get());
			if(lbl != NULL)
			{
				SetLabel(lbl);
			}
		}
	}

	attribute = xml_document->FirstAttribute(node, "disabled_label");
	if(attribute != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> control = FindControl(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
		if(control != NULL)
		{
			UILabel *lbl = dynamic_cast<UILabel*>(control.get());
			if(lbl != NULL)
			{
				SetDisabledLabel(lbl);
			}
		}
	}

	attribute = xml_document->FirstAttribute(node, "overed_label");
	if(attribute != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> control = FindControl(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
		if(control != NULL)
		{
			UILabel *lbl = dynamic_cast<UILabel*>(control.get());
			if(lbl != NULL)
			{
				SetOveredLabel(lbl);
			}
		}
	}

	attribute = xml_document->FirstAttribute(node, "pressed_label");
	if(attribute != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> control = FindControl(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
		if(control != NULL)
		{
			UILabel *lbl = dynamic_cast<UILabel*>(control.get());
			if(lbl != NULL)
			{
				SetPressedLabel(lbl);
			}
		}
	}

	attribute = xml_document->FirstAttribute(node, "focused_label");
	if(attribute != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> control = FindControl(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
		if(control != NULL)
		{
			UILabel *lbl = dynamic_cast<UILabel*>(control.get());
			if(lbl != NULL)
			{
				SetFocusedLabel(lbl);
			}
		}
	}

	attribute = xml_document->FirstAttribute(node, "focused_overed_label");
	if(attribute != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> control = FindControl(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
		if(control != NULL)
		{
			UILabel *lbl = dynamic_cast<UILabel*>(control.get());
			if(lbl != NULL)
			{
				SetFocusedOveredLabel(lbl);
			}
		}
	}
}

void UIButton::OnStateChangedCB()
{
	__super::OnStateChangedCB();
	IUIControlInterface *active_label = NULL;
	if(!IsEnabled())
	{
		active_label = disabled_label_.get();
	}else if(IsFocused())
	{
		if(pressed_)
			active_label = pressed_label_.get();
		else if(overed_)
			active_label = focused_overed_label_.get();
		else
			active_label = focused_label_.get();
	}else if(pressed_)
	{
		active_label = pressed_label_.get();
	}else if(overed_)
	{
		active_label = overed_label_.get();
	}

	if(active_label == NULL)
		active_label = label_.get();

	if(active_label_.get() != active_label)
	{
		if(active_label_.get() != NULL)
			active_label_.get()->SetVisible(false);
		active_label_.reset();
		if(active_label != NULL)
		{
			active_label_ = active_label->as_weak_ptr();
			active_label_.get()->SetVisible(true);
		}
	}
}

HCURSOR UIButton::GetCursor()
{
	return ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
}

void UIButton::SetText(const tstring &text)
{
	text_ = text;
	ApplyTextOnLabel(label_.get());
	ApplyTextOnLabel(disabled_label_.get());
	ApplyTextOnLabel(overed_label_.get());
	ApplyTextOnLabel(pressed_label_.get());
	ApplyTextOnLabel(focused_label_.get());
	ApplyTextOnLabel(focused_overed_label_.get());
}

tstring UIButton::GetText()
{
	return text_;
}

void UIButton::SetLabel(IUIControlInterface *label)
{
	label_.reset();
	if(label == NULL)
		return;
	label_ = label->as_weak_ptr();
	ApplyTextOnLabel(label_.get());
	if(label_.get() != NULL)
		label_.get()->SetVisible(false);
	OnStateChangedCB();
}


void UIButton::SetDisabledLabel(IUIControlInterface *label)
{
	disabled_label_.reset();
	if(label == NULL)
		return;
	disabled_label_ = label->as_weak_ptr();
	ApplyTextOnLabel(disabled_label_.get());
	if(disabled_label_.get() != NULL)
		disabled_label_.get()->SetVisible(false);
	OnStateChangedCB();
}

void UIButton::SetOveredLabel(IUIControlInterface *label)
{
	overed_label_.reset();
	if(label == NULL)
		return;
	overed_label_ = label->as_weak_ptr();
	ApplyTextOnLabel(overed_label_.get());
	if(overed_label_.get() != NULL)
		overed_label_.get()->SetVisible(false);
	OnStateChangedCB();
}

void UIButton::SetPressedLabel(IUIControlInterface *label)
{
	pressed_label_.reset();
	if(label == NULL)
		return;
	pressed_label_ = label->as_weak_ptr();
	ApplyTextOnLabel(pressed_label_.get());
	if(pressed_label_.get() != NULL)
		pressed_label_.get()->SetVisible(false);
	OnStateChangedCB();
}

void UIButton::SetFocusedLabel(IUIControlInterface *label)
{
	focused_label_.reset();
	if(label == NULL)
		return;
	focused_label_ = label->as_weak_ptr();
	ApplyTextOnLabel(focused_label_.get());
	if(focused_label_.get() != NULL)
		focused_label_.get()->SetVisible(false);
	OnStateChangedCB();
}

void UIButton::SetFocusedOveredLabel(IUIControlInterface *label)
{
	focused_overed_label_.reset();
	if(label == NULL)
		return;
	focused_overed_label_ = label->as_weak_ptr();
	ApplyTextOnLabel(focused_overed_label_.get());
	if(focused_overed_label_.get() != NULL)
		focused_overed_label_.get()->SetVisible(false);
	OnStateChangedCB();
}

void UIButton::ApplyTextOnLabel(IUIControlInterface *label)
{
	if(label == NULL)
		return;
	scoped_ref_ptr<UILabel> rlabel = dynamic_cast<UILabel*>(label);
	if(rlabel == NULL)
		return;
	rlabel->SetText(text_);
}

_SSUIL_END
_SSL_END
